package com.guodi.model.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.guodi.core.tool.api.R;
import com.guodi.model.service.ICacheService;
import com.guodi.system.feign.IDictClient;
import com.guodi.system.feign.IUserClient;
import com.guodi.system.vo.DictVO;
import com.guodi.system.vo.UserVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @作者: 林楷
 * @日期: 2022/9/6
 * @描述: 读取字典缓存
 */
@Service
public class CacheServiceImpl implements ICacheService {
    @Resource
    RedisTemplate<String, String> redisTemplate;
    @Resource
    private IDictClient dictClient;
    @Resource
    private IUserClient userClient;
    @Override
    public List<DictVO> getDict(String code) {
        ValueOperations<String, String> operations = redisTemplate.opsForValue();
        String key = "dms:temp:dict:" + code;
        if(redisTemplate.hasKey(key)) {
            String json = operations.get(key);
            if (json != null) {
                return JSONArray.parseArray(json, DictVO.class);
            }
        }
        R<List<DictVO>> r = dictClient.getList(code);
        if(r.isSuccess()) {
            List<DictVO> data = r.getData();
            operations.set(key, JSONArray.toJSONString(data), 60, TimeUnit.SECONDS);
            return data;
        }
        return null;
    }

    @Override
    public String getDictValue(String code, String keys) {
        if(StringUtils.isNotBlank(keys)) {
            List<DictVO> data = this.getDict(code);
            if (data != null) {
                String[] keyArr = keys.split(",");
                StringBuilder value = new StringBuilder();
                for (String key : keyArr) {
                    for (DictVO dict : data) {
                        if (dict.getDictKey().equals(key)) {
                            value.append(",").append(dict.getDictValue());
                        }
                    }
                }
                return value.length() > 0 ? value.substring(1) : null;
            }
        }
        return keys;
    }

    @Override
    public String getUserName(Long userId) {
        ValueOperations<String, String> operations = redisTemplate.opsForValue();
        String key = "dms:temp:username:" + userId;
        try {
            if(redisTemplate.hasKey(key)) {
                return operations.get(key);
            }
            R<UserVO> r = userClient.detailById(userId);
            if (r.isSuccess()) {
                operations.set(key, r.getData().getUserName(), 60, TimeUnit.SECONDS);
                return r.getData().getUserName();
            }
        } catch (Exception e) {
            operations.set(key, "-1", 60, TimeUnit.SECONDS);
            e.printStackTrace();
        }
        return "-1";
    }
}
